<template>
  <!-- 系统管理-角色管理 -->
  <div class="app-container">
    <div class="search-div">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="角色名称">
          <el-input v-model="form.roleName" style="width: 200px" placeholder="请输入角色名称" clearable/>
        </el-form-item>
        <el-form-item label="权限名称">
          <el-input v-model="form.permissionName" style="width: 200px" placeholder="请输入权限名称" clearable/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-div">
      <div class="table-oper-div">
        <el-button type="success" :icon="Plus" @click="openDialog('add')" circle/>
        <el-button type="primary" :icon="Edit" @click="openDialog('edit')" circle/>
        <el-button type="danger" :icon="Delete" @click="deleteRole"  circle/>
      </div>
      <el-table
          ref="multipleTableRef"
          :data="tableData"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          stripe
          border
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column property="roleName" label="角色名称"/>
        <el-table-column property="roleType" label="角色标识"/>
        <el-table-column property="roleDesc" label="角色描述"/>
        <el-table-column property="permissionName" label="权限列表"/>
        <el-table-column property="status" label="角色状态">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 1" type="success">启动</el-tag>
            <el-tag v-else type="danger">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column property="remark" label="备注"/>
      </el-table>
      <el-pagination :page-size="form.pageSize" v-model:current-page="form.pageNum"
                     @current-change="handleCurrentChange" layout="prev, pager, next" v-model:total="form.total"/>
    </div>
    <!--  新增、编辑弹窗 -->
    <el-dialog
        v-model="dialogVisible"
        :title="dialogTitle"
        width="600"
        :destroy-on-close="true"
    >
      <role-edit :form="editForm" :echo-permissions="echoPermissions" @save="saveCallBack"
                 @close="dialogVisible = false"></role-edit>
    </el-dialog>
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";
import {deleteRoleApi, getRolePageApi} from "@/assets/api/system/role";
import {Delete, Edit, Plus} from "@element-plus/icons-vue";
import RoleEdit from "@/components/view/system/role/roleEdit.vue";
import {ElMessage, ElMessageBox} from "element-plus";

// 查询条件
const form = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0,
  roleName: '',
  permissionName: ''
});

const onSubmit = () => {
  getTableData();
};

const onReset = () => {
  form.value.roleName = '';
  form.value.permissionName = '';
};

// 全选按钮
const multipleTableRef = ref()
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

// 获取角色列表
const tableData = ref([]);
const getTableData = () => {
  getRolePageApi(form.value).then(res => {
    tableData.value = res.data.data.records;
    form.value.total = res.data.data.total;
  });
}

// 页码切换处理器
const handleCurrentChange = () => {
  getTableData();
}

// 新增、编辑弹窗
const dialogVisible = ref(false)
const dialogTitle = ref('')
const editForm = ref({}); // 编辑的数据
const echoPermissions = ref([]); // 编辑时回显的权限
const openDialog = (tag) => {
  if (tag === 'edit' && multipleSelection.value.length !== 1) {
    ElMessage({
      message: '请选择一条数据进行编辑',
      type: 'warning'
    });
    return;
  }
  dialogTitle.value = tag === 'add' ? '新增角色' : '编辑角色';
  echoPermissions.value = [];
  if (tag === 'edit') {
    editForm.value = JSON.parse(JSON.stringify(multipleSelection.value[0]));
    echoPermissions.value = editForm.value.permissions.map(item => item.id);
  } else {
    editForm.value = {};
  }
  dialogVisible.value = true;
}

// 编辑修改回调
const saveCallBack = (saved) => {
  if (saved) {
    dialogVisible.value = false;
    getTableData();
  }
}

// 删除角色
const deleteRole = () => {
   if (multipleSelection.value.length === 0) {
    ElMessage({
      message: '请选择一条数据进行删除',
      type: 'warning'
    });
    return;
  }
  ElMessageBox.confirm(
      '确认要删除吗？',
      '重要提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    // 将对象中的id取出组成新的数组，然后使用join拼接
    const ids = multipleSelection.value.map(item => item.id);
    deleteRoleApi(ids.join(",")).then(res => {
      if (res.data.code === 200) {
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        getTableData();
      } else {
        ElMessage({
          type: 'error',
          message: res.data.msg,
        })
      }
    })
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消删除',
    })
  })
}

onMounted(() => {
  getTableData();
})
</script>

<style scoped>
.app-container {
  padding: 10px;
}

.table-oper-div {
  padding-left: 15px;
  padding-bottom: 10px;
}
</style>